﻿using ParkLBS.FrameWork;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;

namespace ParkLBS.Controller
{
    /// <summary>
    /// Http帮助类
    /// </summary>
    class HttpUtility
    {
        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="url">Url地址</param>
        /// <param name="data">数据</param>
        public static string SendHttpRequest(string url, string data)
        {
            return SendPostHttpRequest(url, "application/x-www-form-urlencoded", data);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string GetData(string url)
        {
            return SendGetHttpRequest(url, "application/x-www-form-urlencoded");
        }

        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="url">Url地址</param>
        /// <param name="method">方法（post或get）</param>
        /// <param name="method">数据类型</param>
        /// <param name="requestData">数据</param>
        public static string SendPostHttpRequest(string url, string contentType, string requestData)
        {
            string result = string.Empty;
            try
            {
                WebRequest request = (WebRequest)HttpWebRequest.Create(url);
                request.Method = "POST";
                byte[] postBytes = null;
                request.ContentType = contentType;
                postBytes = Encoding.UTF8.GetBytes(requestData);
                request.ContentLength = postBytes.Length;
                using (Stream outstream = request.GetRequestStream())
                {
                    outstream.Write(postBytes, 0, postBytes.Length);
                }

                using (WebResponse response = request.GetResponse())
                {
                    if (response != null)
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                               
                                result = reader.ReadToEnd();
                                clsLogHelper.WriteLog(null, null, result);
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {

                clsLogHelper.WriteLog(null, null, ex.ToString());
            }
            
            return result;
        }

        /// <summary>
        /// 发送请求
        /// </summary>
        /// <param name="url">Url地址</param>
        /// <param name="method">方法（post或get）</param>
        /// <param name="method">数据类型</param>
        /// <param name="requestData">数据</param>
        public static string SendGetHttpRequest(string url, string contentType)
        {
            WebRequest request = (WebRequest)HttpWebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = contentType;
            string result = string.Empty;
            using (WebResponse response = request.GetResponse())
            {
                if (response != null)
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                        {
                            result = reader.ReadToEnd();
                        }
                    }
                }
            }
            return result;
        }
    }
}